//
//  XKUserAccount.swift
//  XKWeibo
//
//  Created by 小k on 15/11/7.
//  Copyright © 2015年 小k. All rights reserved.
//

import UIKit

/// 用户创建模型
class XKUserAccount: NSObject, NSCoding{
    
    var access_token: String?
    
    // 计算过期日期
    var expiresDate: NSDate?
    
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    // 授权的UID
    var uid:String?
    
    // 用户名称
    var screen_name:String?
    
    // 用户头像
    var avatar_large:String?
    
    // 重写构造方法
    init(dict:[String:AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    // 避免键值对不匹配
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // 输出模型详细信息  --->description
    override var description: String {
        let keys = ["access_token", "expires_in", "expiresDate", "uid", "screen_name", "avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    /// 归档, 在对象保存之前, 将对象转换成二进制数据
    func encodeWithCoder(aCoder: NSCoder) {
            aCoder.encodeObject(access_token, forKey: "access_token")
            aCoder.encodeObject(avatar_large, forKey: "avatar_large")
            aCoder.encodeObject(expiresDate, forKey: "expiresDate")
            aCoder.encodeObject(uid, forKey: "uid")
            aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    /// 解档, 加载二进制文件是 调用,  required  没有继承性, 保证所有对象只能解档出当前类的对象
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        
    }
    
}
